package com.sctvcloud.bazhong.ui.activities;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Pair;
import android.view.View;
import android.webkit.ValueCallback;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.ruihang.generalibrary.ui.dialog.ShareFragment;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.sctvcloud.bazhong.R;
import com.sctvcloud.bazhong.base.BaseDetailActivity;
import com.sctvcloud.bazhong.ui.widget.MyWebChromeClient;


import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CityH5Activity extends BaseDetailActivity {
    @BindView(R.id.webView_h5)
    protected WebView webView;
    @BindView(R.id.title_top_layout_edit)
    protected CustomFontTextView edit;


    private String url;
    private String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        new TitleUtils(this);
        edit.setVisibility(View.GONE);
        initView();
    }

    @Override
    protected void initInject() {
        setContentView(R.layout.activity_h5);
        title = getIntent().getStringExtra(EX_TITLE);
        ButterKnife.bind(this);
    }

    private void initView() {
        WebSettings settings = webView.getSettings();
        // 使用缓存
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        String ua = settings.getUserAgentString();
        settings.setUserAgentString(ua + " SCGC_UA");
//        settings.getUserAgentString()
        //使h5页面适应手机屏幕
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        //设置可以支持缩放
        settings.setSupportZoom(true);
        //设置出现缩放工具
        settings.setBuiltInZoomControls(true);
        //隐藏缩放工具
        settings.setDisplayZoomControls(false);
        //扩大比例的缩放
        settings.setUseWideViewPort(true);
        // 支持JavaScript
        settings.setJavaScriptEnabled(true);
//        settings.setMediaPlaybackRequiresUserGesture(false);
        webView.setWebChromeClient(new MyWebChromeClient());
        webView.setWebViewClient(new android.webkit.WebViewClient() {
            /**
             * 防止加载网页时调起系统浏览器
             */
            public boolean shouldOverrideUrlLoading(android.webkit.WebView view, String url) {
                if (!url.startsWith("http://")&&!url.startsWith("https://")) {
                    return true;
                }
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(android.webkit.WebView view, String url) {
                super.onPageFinished(view, url);
                String titleStr = view.getTitle();
                if (!TextUtils.isEmpty(titleStr)) {
//                    if (TextUtils.isEmpty(title)){
//                    title = titleStr;
//                    }
                }
            }
        });
        Intent intent = getIntent();
        if (intent != null) {
            url = intent.getStringExtra(EX_HTML);
            if (!TextUtils.isEmpty(url)) {
                webView.loadUrl(url);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (webView != null) {
            webView.removeAllViews();
            webView.destroy();
        }
    }

    @OnClick({R.id.title_top_layout_back})
    public void itemClick(View view) {
        switch (view.getId()) {
            case R.id.title_top_layout_back:
                finish();
                break;
        }
    }
}
